private var scrollPosition = Vector2.zero;
private var entries = ArrayList();
public var nick = "Nick";
private var msg = "Mensagem";
private var msg2="Mensagem";
private var status = "";
private var startTime;
public var compPlayers : Players;

function Awake() {
	startTime = Time.time;
}

function OnGUI () {
	nick = GUI.TextField(Rect(0,100,100,25),nick,25);
	rectWin = GUI.Window(1,Rect (0,130,300,500),WinChat,"Chat");
	msg2 = GUI.TextField(Rect(0,640,240,25),msg,25);
	if (GUI.Button(Rect(250,640,50,25),"Send")) {
		for (var i=0; i<compPlayers.players.Count; i++) {
			var p : Player = compPlayers.players[i];
			if (p.Address == Network.player) {
				sendMsg(nick,msg);
			}else if (p.toggleOn) {
				networkView.RPC("sendMsg",p.Address,nick,msg);
			}
		}
	}
	GUI.Label(Rect(0,670,240,25),status);
	
	//var e: Event = Event.current;
	//if (e.isKey) {
	if (msg2 != msg) {
		networkView.RPC("mudaStatus",RPCMode.Others,nick+" está digitando");
		msg = msg2;
	}
}

function WinChat(id : int) {
	scrollPosition = GUILayout.BeginScrollView(scrollPosition);
	for (var entry : Entry in entries) {
		//GUILayout.BeginVertical();
		var m = entry.sender + ": " + entry.msg;
		GUILayout.Label(m);
		GUILayout.FlexibleSpace();
		//GUILayout.EndVertical();
	}
	GUILayout.EndScrollView();
}

@RPC
function mudaStatus(msg: String) {
	status = msg;
	startTime = Time.time;
}

function Update () {
	var time = Time.time;
	if (time - startTime > 3) {
		status = "";
	}
}

@RPC
function sendMsg(nick: String, msg: String) {
	var entry = new Entry();
	entry.msg = msg;
	entry.sender = nick;
	entries.Add(entry);
}